package history.study.array;

// 844. 比较含退格的字符串:https://leetcode-cn.com/problems/backspace-string-compare/
public class LeetCode_844 {
    public static void main(String[] args) {
        System.out.println(backspaceCompare("ab#c","ad#c"));
    }

    public static boolean backspaceCompare(String s, String t) {
        if (s == null && t == null) return true;
        if (s == null || t == null) return false;
        return getString(s.toCharArray()).equals(getString(t.toCharArray()));
    }

    private static String getString(char[] s) {
        int slow = 0;
        for (int i = 0; i < s.length; i++) {
            if (s[i]=='#') {
                slow = slow==0?0:--slow;
            } else {
                s[slow++] = s[i];
            }
        }
        return  String.valueOf(s).substring(0,slow);
    }
}
